﻿Imports System.Windows.Forms

Public Class frmLatLon

    Private Sub btnApply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnApply.Click

        SaveSetting(REGAPP, Me.Name, tabMain.Name, tabMain.SelectedIndex)
        SaveSetting(REGAPP, Me.Name, cboLat.Name, cboLat.Text)
        SaveSetting(REGAPP, Me.Name, cboLon.Name, cboLon.Text)
        SaveSetting(REGAPP, Me.Name, cboSpheroid.Name, cboSpheroid.Text)
        SaveSetting(REGAPP, Me.Name, cboZone.Name, cboZone.Text)
        SaveSetting(REGAPP, Me.Name, cboHemi.Name, cboHemi.Text)

        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub frmLatLon_Load(sender As Object, e As EventArgs) Handles Me.Load
        tabMain.SelectedIndex = GetSetting(REGAPP, Me.Name, tabMain.Name, 0)
        cboLat.Items.AddRange(New String() {"N", "S"})
        cboLat.Text = GetSetting(REGAPP, Me.Name, cboLat.Name, "N")
        cboLon.Items.AddRange(New String() {"W", "E"})
        cboLon.Text = GetSetting(REGAPP, Me.Name, cboLon.Name, "W")
        cboSpheroid.Items.AddRange(clsCoordinates.Ellipsoids.ToArray)
        cboSpheroid.Text = GetSetting(REGAPP, Me.Name, cboSpheroid.Name, "Clarke 1866")
        For i As Integer = 1 To 60
            cboZone.Items.Add(i)
        Next
        cboZone.Text = GetSetting(REGAPP, Me.Name, cboZone.Name, "17")
        cboHemi.Items.AddRange(New String() {"N", "S"})
        cboHemi.Text = GetSetting(REGAPP, Me.Name, cboHemi.Name, "N")
    End Sub

    Private Sub frmLatLon_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        If tabMain.SelectedIndex = 0 Then txtLatD.Focus() Else txtX.Focus()
    End Sub

    Private Sub ComputeLatLon()
        txtLat.Text = String.Format("{0:0.00000}", (Val(txtLatD.Text) + Val(txtLatM.Text) / 60 + Val(txtLatS.Text) / 3600) * IIf(cboLat.Text = "S", -1, 1))
        txtLon.Text = String.Format("{0:0.00000}", (Val(txtLonD.Text) + Val(txtLonM.Text) / 60 + Val(txtLonS.Text) / 3600) * IIf(cboLon.Text = "W", -1, 1))
    End Sub

    Private Sub txt_Enter(sender As Object, e As EventArgs) Handles txtLatD.Enter, txtLatM.Enter, txtLatS.Enter, txtLonD.Enter, txtLonM.Enter, txtLonS.Enter, txtX.Enter, txtY.Enter
        BeginInvoke(New Action(Sub()
                                   CType(sender, TextBox).SelectAll()
                               End Sub))
    End Sub

    Private Sub txtLatD_Validated(sender As Object, e As EventArgs) Handles txtLatD.Validated
        txtLatD.Text = Math.Min(89, Math.Max(0, CInt(Val(txtLatD.Text))))
        ComputeLatLon()
    End Sub

    Private Sub txtLonD_Validated(sender As Object, e As EventArgs) Handles txtLonD.Validated
        txtLonD.Text = Math.Min(180, Math.Max(0, CInt(Val(txtLonD.Text))))
        ComputeLatLon()
    End Sub

    Private Sub txtLatM_Validated(sender As Object, e As EventArgs) Handles txtLatM.Validated, txtLonM.Validated
        Dim txt As TextBox = sender
        txt.Text = Math.Min(59, Math.Max(0, CInt(Val(txt.Text))))
        ComputeLatLon()
    End Sub

    Private Sub txtLatS_Validated(sender As Object, e As EventArgs) Handles txtLatS.Validated, txtLonS.Validated
        Dim txt As TextBox = sender
        txt.Text = Math.Min(59.999, Math.Max(0, Val(txt.Text)))
        ComputeLatLon()
    End Sub

    Private Sub cboSpheroid_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboSpheroid.SelectedIndexChanged, cboZone.SelectedIndexChanged, cboHemi.SelectedIndexChanged, txtX.TextChanged, txtY.TextChanged
        If IsNumeric(txtX.Text) And IsNumeric(txtY.Text) Then
            Dim crd As New clsCoordinates(cboSpheroid.SelectedIndex, cboZone.Text, IIf(cboHemi.Text = "N", clsCoordinates.enumNorthSouth.North, clsCoordinates.enumNorthSouth.South))
            With crd.UTMtoLatLon(Val(txtX.Text), Val(txtY.Text))
                txtLat.Text = String.Format("{0:0.00000}", .Y)
                txtLon.Text = String.Format("{0:0.00000}", .X)
            End With
        End If
    End Sub


End Class
